//========encoding:utf-8=========
/*
 * @describe:  三目运算符
 * @author:mhao
 * @copyright:chinasoft.cn
 * @date:2020年4月1日   上午10:54:14
 */
package edu.chinasoft.handler;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import edu.chinasoft.domain.Application;
import edu.chinasoft.domain.Approval;
import edu.chinasoft.domain.Role;
import edu.chinasoft.domain.User;
import edu.chinasoft.service.ApplicationService;
import edu.chinasoft.service.ApprovalService;
import edu.chinasoft.util.Dom4jUtil;

@Controller("approvalHandler")
@RequestMapping("/approvalHandler")
public class ApprovalHandler {
	private static Logger logger = LoggerFactory.getLogger(ApprovalHandler.class);
	@Autowired
	@Qualifier("applicationService")
	private ApplicationService applicationService;
	@Autowired
	@Qualifier("approvalService")
	private ApprovalService approvalService;
	//填写审核表
    @RequestMapping(value = "/getUserForAddApproval.do",method = {RequestMethod.POST,RequestMethod.GET})
    public ModelAndView getUserForAddApproval(Integer applicationId,ModelAndView modelAndView, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info(" ApprovalHandler >>> getUserForAddApproval start.......");
        logger.info("当前申请单的id为："+applicationId);
        session.setAttribute("applicationId",applicationId);
        //获取当前用户的下一级审批者
        //获取当前登录用户
        User userLogin = (User) session.getAttribute("userLogin");
        //获取当前登录用户的角色
        Role LoginRole = applicationService.selectRoleByUser(userLogin);
        //获取xml文件的绝对路径
        String path = "/config/flow.xml";
        String realPath = request.getServletContext().getRealPath(path);
        List<String> roleNameList = Dom4jUtil.getContent(realPath);
        int index = roleNameList.indexOf(LoginRole.getRoleName());
        if((index+1)<roleNameList.size()) {
        	//存在下一级审批者
        	String roleName = roleNameList.get(index);
        	Role role = new Role();
        	role.setRoleName(roleName);
        	//获取角色对应的用户--下一级审批者
        	List<User> userList = applicationService.selectUserByRole(role);
        	session.setAttribute("approvalUserInfo", userList);
        	logger.info("下一级审批者有："+userList);
        }
        Object approvalUserInfo = session.getAttribute("approvalUserInfo");
        logger.info("下一级审批者有："+approvalUserInfo);
        modelAndView.setViewName("/addApproval");
        logger.info(" ApprovalHandler >>> getUserForAddApproval end.......");
		return modelAndView;
    
	}
    public static Approval addApproval(Approval approval, HttpSession session, HttpServletRequest request) {
        logger.info(" ApprovalHandler >>> addApproval start.......");
        //获取当前时间
        Date currentDate = new Date();
        logger.info("当前时间为："+currentDate);
        approval.setApprovalTime(currentDate);
        //获取当前登录用户
        User userLogin = (User) session.getAttribute("userLogin");
        approval.setApprovalUser(userLogin);
        //获取当前申请表id
        String applicationId = request.getParameter("applicationId");
        Application application = new Application();
        application.setApplicationId(Integer.parseInt(applicationId));
        //设置申请表状态为0 结束
        application.setApplicationState("0");
        approval.setApplication(application);

        logger.info(" ApprovalHandler >>> addApproval end.......");
        return approval;
    }
	//驳回
    @RequestMapping(value="/addApprovalForReject.do",method= {RequestMethod.POST,RequestMethod.GET})
    public String addApprovalForReject(Approval approval,String approvalContent,HttpServletRequest request,HttpServletResponse response,HttpSession session) throws IOException{
    	 logger.info(" ApprovalHandler >>> addApprovalForReject start.......");
    	 Approval addApproval = ApprovalHandler.addApproval(approval, session, request);
    	 boolean result = approvalService.insertApproval(addApproval);
    	 if(!result) {
    		 return "/addapprovalerror";
    	 }
    	 logger.info(" ApprovalHandler >>> addApprovalForReject end.......");
		return "redirect:/applicationHandler/showApprovalApplication.do";
    	
    }
    //批准
    @RequestMapping(value="/addApprovalForApprove.do",method= {RequestMethod.POST,RequestMethod.GET})
    public String addApprovalForApprove(HttpServletRequest request,HttpServletResponse response,HttpSession session,Approval approval) throws IOException{
    	logger.info(" ApprovalHandler >>> addApprovalForApprove start.......");
    	Approval addApproval = ApprovalHandler.addApproval(approval, session, request);
    	//获取用户填写的下一级审批者 申请表id 当前用户id
    	String nextUserName = request.getParameter("selectapprovaluser");
    	logger.info("下一级审批者："+nextUserName);
    	if(nextUserName!=null) {
    		//存在下一级审批者
    		User nextUser = new User();
    		nextUser.setUserName(nextUserName);
    		approval.setNextApprovalUser(nextUser);
    		//设置申请表状态为2 结束
    		Application application = approval.getApplication();
    		application.setApplicationState("2");
    		approval.setApplication(application);
    	}
    	boolean result = approvalService.insertApproval(approval);
    	if(!result) {
    		return "/addapprovalerror";
    	}
    	logger.info(" ApprovalHandler >>> addApprovalForApprove end.......");
		return "redirect:/applicationHandler/showApprovalApplication.do";
    	
    }
	@InitBinder
    protected void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }
}
